package com.api.customization.unionpay.market.web;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.service.ApplicantUpdateService;
import com.customization.unionpay.market.service.impl.ApplicantUpdateServiceImpl;
import com.customization.unionpay.market.util.FormDataUtils;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.Map;

/**
 * @author sjm
 * @description 修改台账人员相关
 */
@Path("/market/update")
public class ApplicantUpdateController {

    private static final Logger log = LoggerFactory.getLogger(ApplicantUpdateController.class);

    private ApplicantUpdateService getService(User user) {
        return ServiceUtil.getService(ApplicantUpdateServiceImpl.class, user);
    }

    // 营销项目管理平台修改申请人
    @POST
    @Path("/applicant")
    @Produces(MediaType.APPLICATION_JSON)
    public String externalUpdateApplicant(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject result = new JSONObject();
        try {
            result.put("msg", "修改成功");
            result.put("data", null);
            result.put("code", 200);
            result.put("errorCode", 1);
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);

            String body = FormDataUtils.readBodyUTF8(request);

            log.info("请求体参数：" + body);
            if (body != null) {
                JSONObject parseJson = JSONObject.parseObject(body);

                String code = parseJson.getString("SERIAL_CODE");
                String oldApplicant = parseJson.getString("OLD_APPLICANT");
                String newApplicant = parseJson.getString("NEW_APPLICANT");
                String source = parseJson.getString("SOURCE");
                String time = parseJson.getString("TIME");
                String actAuthCode = parseJson.getString("ACT_AUTH_CODE");

                if (StrUtil.isBlank(code)) {
                    result.put("msg", "项目编号为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(oldApplicant)) {
                    result.put("msg", "旧申请人为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(newApplicant)) {
                    result.put("msg", "新申请人为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(actAuthCode)) {
                    result.put("msg", "授权码为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(source)) {
                    result.put("msg", "来源系统为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(time)) {
                    result.put("msg", "请求时间为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                getService(user).externalUpdateApplicant(code, actAuthCode, newApplicant);
            } else {
                result.put("msg", "参数异常");
                result.put("data", null);
                result.put("code", 500);
                result.put("errorCode", 10003);
            }
        } catch (Exception e) {
            log.info("接口异常：" + e.getMessage());
            result.put("msg", "接口异常，请联系管理员！");
            result.put("data", null);
            result.put("code", 500);
            result.put("errorCode", 5000);
        }
        return JSONObject.toJSONString(result);
    }

    // 合同模块修改活动管理人
    @POST
    @Path("/eventManager")
    @Produces(MediaType.APPLICATION_JSON)
    public String internalUpdateEventManager(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject result = new JSONObject();
        try {
            result.put("msg", "修改成功");
            result.put("data", null);
            result.put("code", 200);
            result.put("errorCode", 1);
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);

            String body = FormDataUtils.readBodyUTF8(request);

            log.info("请求体参数：" + body);
            if (body != null) {
                JSONObject parseJson = JSONObject.parseObject(body);

                String newApplicant = parseJson.getString("NEW_APPLICANT");
                String actAuthCode = parseJson.getString("ACT_AUTH_CODE");
                String source = parseJson.getString("SOURCE");
                String time = parseJson.getString("TIME");

                if (StrUtil.isBlank(newApplicant)) {
                    result.put("msg", "新申请人为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(actAuthCode)) {
                    result.put("msg", "授权码为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(source)) {
                    result.put("msg", "来源系统为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(time)) {
                    result.put("msg", "请求时间为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }

                getService(user).internalUpdateEventManager(actAuthCode, newApplicant);
            } else {
                result.put("msg", "参数异常");
                result.put("data", null);
                result.put("code", 500);
                result.put("errorCode", 10003);
            }

        } catch (Exception e) {
            log.info("接口异常：" + e.getMessage());
            result.put("msg", "接口异常，请联系管理员！");
            result.put("data", null);
            result.put("code", 500);
            result.put("errorCode", 5000);
        }
        return JSONObject.toJSONString(result);
    }

    // 根据组织架构唯一值转换人员id
    @POST
    @Path("/changeUserId")
    @Produces(MediaType.APPLICATION_JSON)
    public String changeUserIdByCode(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject result = new JSONObject();
        try {
            result.put("msg", "查询成功");
            result.put("data", null);
            result.put("code", 200);
            result.put("errorCode", 1);
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);

            String body = FormDataUtils.readBodyUTF8(request);
            log.info("请求体参数：" + body);
            if (body != null) {
                JSONObject parseJson = JSONObject.parseObject(body);
                String codeStr = parseJson.getString("code");
                if (StrUtil.isBlank(codeStr) || "".equals(codeStr)) {
                    result.put("msg", "未获取人员唯一值！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }

                JSONObject data =  getService(user).getChangeUserIdByCode(user,codeStr);
                result.put("data", data);
            } else {
                result.put("msg", "参数异常");
                result.put("data", null);
                result.put("code", 500);
                result.put("errorCode", 10003);
            }
        } catch (Exception e) {
            log.info("接口异常：" + e.getMessage());
            result.put("msg", "接口异常，请联系管理员！");
            result.put("data", null);
            result.put("code", 500);
            result.put("errorCode", 5000);
        }
        return JSONObject.toJSONString(result);
    }

    @GET
    @Path("/getSqm")
    @Produces({MediaType.TEXT_PLAIN})
    public String getSqmCode(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject result = new JSONObject();
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            result.put("msg", "查询成功");
            result.put("data", null);
            result.put("code", 200);
            result.put("errorCode", 1);
            Map req = ParamUtil.request2Map(request);
            String code = req.get("code") != null ? req.get("code").toString() : "";
            String data =  getService(user).getSqmCode(code);
            result.put("data", data);

        } catch (Exception e) {
            log.info("接口异常：" + e.getMessage());
            result.put("msg", "接口异常，请联系管理员！"+ e.getMessage());
            result.put("data", null);
            result.put("code", 500);
            result.put("errorCode", 5000);
        }
        return JSONObject.toJSONString(result);
    }

    @GET
    @Path("/initSqm")
    @Produces({MediaType.TEXT_PLAIN})
    public String initSqmCode(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject result = new JSONObject();
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            result.put("msg", "生成成功");
            result.put("data", null);
            result.put("code", 200);
            result.put("errorCode", 1);
            String data =  getService(user).initSqmCode();
            result.put("data", data);

        } catch (Exception e) {
            log.info("接口异常：" + e.getMessage());
            result.put("msg", "接口异常，请联系管理员！"+ e.getMessage());
            result.put("data", null);
            result.put("code", 500);
            result.put("errorCode", 5000);
        }
        return JSONObject.toJSONString(result);
    }

    @GET
    @Path("/updateSqm")
    @Produces({MediaType.TEXT_PLAIN})
    public String updateSqm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject result = new JSONObject();
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            result.put("msg", "调整成功");
            result.put("data", null);
            result.put("code", 200);
            result.put("errorCode", 1);
            Map req = ParamUtil.request2Map(request);
            String flag = req.get("flag") != null ? req.get("flag").toString() : "";
            String data =  getService(user).updateSqm(flag);
            result.put("data", data);

        } catch (Exception e) {
            log.info("接口异常：" + e.getMessage());
            result.put("msg", "接口异常，请联系管理员！"+ e.getMessage());
            result.put("data", null);
            result.put("code", 500);
            result.put("errorCode", 5000);
        }
        return JSONObject.toJSONString(result);
    }

    @GET
    @Path("/clearJiZ")
    @Produces({MediaType.TEXT_PLAIN})
    public String clearJiZ(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject result = new JSONObject();
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            result.put("msg", "清算成功");
            result.put("data", null);
            result.put("code", 200);
            result.put("errorCode", 1);
            String data =  getService(user).clearJiZ();
            result.put("data", data);

        } catch (Exception e) {
            log.info("接口异常：" + e.getMessage());
            result.put("msg", "接口异常，请联系管理员！"+ e.getMessage());
            result.put("data", null);
            result.put("code", 500);
            result.put("errorCode", 5000);
        }
        return JSONObject.toJSONString(result);
    }

    @GET
    @Path("/createInn")
    @Produces({MediaType.TEXT_PLAIN})
    public String createInnCode(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject result = new JSONObject();
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            result.put("msg", "生成成功");
            result.put("data", null);
            result.put("code", 200);
            result.put("errorCode", 1);
            String data =  getService(user).createInn();
            result.put("data", data);

        } catch (Exception e) {
            log.info("接口异常：" + e.getMessage());
            result.put("msg", "接口异常，请联系管理员！"+ e.getMessage());
            result.put("data", null);
            result.put("code", 500);
            result.put("errorCode", 5000);
        }
        return JSONObject.toJSONString(result);
    }


    @GET
    @Path("/selectPstate")
    @Produces({MediaType.TEXT_PLAIN})
    public String selectPstate(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject result = new JSONObject();
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            result.put("msg", "生成成功");
            result.put("data", null);
            result.put("code", 200);
            result.put("errorCode", 1);
            Map req = ParamUtil.request2Map(request);
            String code = req.get("code") != null ? req.get("code").toString() : "";
            String data =  getService(user).selectPstate(code);
            result.put("data", data);

        } catch (Exception e) {
            log.info("接口异常：" + e.getMessage());
            result.put("msg", "接口异常，请联系管理员！"+ e.getMessage());
            result.put("data", null);
            result.put("code", 500);
            result.put("errorCode", 5000);
        }
        return JSONObject.toJSONString(result);
    }

    // 根据合同编号转换经济事项id
    @POST
    @Path("/changeJJSXId")
    @Produces(MediaType.APPLICATION_JSON)
    public String changeJJSXIdByCode(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject result = new JSONObject();
        try {
            result.put("msg", "查询成功");
            result.put("data", null);
            result.put("code", 200);
            result.put("errorCode", 1);
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);

            String body = FormDataUtils.readBodyUTF8(request);
            log.info("请求体参数：" + body);
            if (body != null) {
                JSONObject parseJson = JSONObject.parseObject(body);

                String codeStr = parseJson.getString("code");
                if (StrUtil.isBlank(codeStr) || "".equals(codeStr)) {
                    result.put("data", "");
                }else {
                    String data =  getService(user).getJJSXIdByCode(user,codeStr);
                    result.put("data", data);
                }
            } else {
                result.put("msg", "参数异常");
                result.put("data", null);
                result.put("code", 500);
                result.put("errorCode", 10003);
            }
        } catch (Exception e) {
            log.info("接口异常：" + e.getMessage());
            result.put("msg", "接口异常，请联系管理员！");
            result.put("data", null);
            result.put("code", 500);
            result.put("errorCode", 5000);
        }
        return JSONObject.toJSONString(result);
    }
}
